package com.dq.service.record.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.CusServiceQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.org.Org;
import com.dq.domain.record.ServiceRecord;
import com.dq.repository.BaseRepositories;
import com.dq.service.org.OrgService;
import com.dq.service.record.CusServicesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuhui
 * @date 2019/11/25 0025 17:44:55
 */
@Service
public class CusServicesServiceImpl extends BaseRepositories implements CusServicesService {
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private OrgService orgService;

    @Override
    public List<ServiceRecord> list(CusServiceQuery cusServiceQuery) throws AdminException {
        Map<String, Object> query = getListQuery(cusServiceQuery);

        List<ServiceRecord> list = dao.find(dao.query2Hql(ServiceRecord.class, query), query);

        return list;
    }

    @Override
    public Long count(CusServiceQuery cusServiceQuery) throws AdminException {
        Map<String, Object> query = getListQuery(cusServiceQuery);

        Long count = dao.count(dao.count2Hql(ServiceRecord.class, query), query);

        return count;
    }

    public Map<String, Object> getListQuery(CusServiceQuery cusServiceQuery) throws AdminException {
        cusServiceQuery.format();
        Map<String, Object> query = cusServiceQuery.parseMap();
        if(cusServiceQuery.getOrgId() != null){
            query.put("sysDept_id",cusServiceQuery.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("sysDept" + BaseDaoImpl.inSqlStr, orgList);
        }

        if(StringUtils.isNotEmpty(cusServiceQuery.getStaffRealCName())){
            query.put("staffUser_userRealcName" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(cusServiceQuery.getStaffRealCName()));
            query.remove("staffRealCName");
        }

        if(cusServiceQuery.getWxUserId()!=null){
            query.put("wxUser_id",cusServiceQuery.getWxUserId());
            query.remove("wxUserId");
        }
        if(StringUtils.isNotEmpty(cusServiceQuery.getWxUserCardNo())){
            query.put("wxUser_cardNo" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(cusServiceQuery.getWxUserCardNo()));
            query.remove("wxUserCardNo");
        }
        if(StringUtils.isNotEmpty(cusServiceQuery.getWxUserUserRealName())){
            query.put("wxUser_userRealcName" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(cusServiceQuery.getWxUserUserRealName()));
            query.remove("wxUserUserRealName");
        }
        if(cusServiceQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,cusServiceQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(cusServiceQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,cusServiceQuery.getEndTime());
            query.remove("endTime");
        }
        if(StringUtils.isNotEmpty(cusServiceQuery.getProjectName())){
            query.put("projectName"+BaseDaoImpl.likeSqlStr,"%"+cusServiceQuery.getProjectName()+"%");
            query.remove("projectName");
        }
        if(StringUtils.isNotEmpty(cusServiceQuery.getProjectCategoryName())){
            query.put("projectCategoryName"+BaseDaoImpl.likeSqlStr,"%"+cusServiceQuery.getProjectCategoryName()+"%");
            query.remove("projectCategoryName");
        }
        if(cusServiceQuery.getStaffUserId()!=null){
            query.put("staffUser_id",cusServiceQuery.getStaffUserId());
            query.remove("staffUserId");
        }
        if(StringUtils.isNotEmpty(cusServiceQuery.getValue())){
            Map<String, Object> keyQuery = new HashMap<>();
            keyQuery.put("wxUser_userRealcName"+ BaseDaoImpl.likeSqlStr, "%"+BaseAdminQuery.likeSql(cusServiceQuery.getValue())+"%");
            keyQuery.put("wxUser_cardNo"+ BaseDaoImpl.likeSqlStr, "%"+BaseAdminQuery.likeSql(cusServiceQuery.getValue())+"%");
            keyQuery.put("wxUser_username" + BaseDaoImpl.likeSqlStr, "%"+BaseAdminQuery.likeSql(cusServiceQuery.getValue())+"%");
            query.put("keyQuery" + BaseDaoImpl.orAndSqlStr, keyQuery);
            query.remove("value");
        }


        query.remove("type");
        return query;
    }

}
